<?php
/*
Plugin Name: Tin theo danh mục (dành cho sidebar tab2)
Description: Hiển thị tin theo danh mục (đặt vào tab2).
Author: Nguyennb - ttrhoabinh@gmail.com
*/

// Start class category_posts_widget //

class category_posts_tab2_widget extends WP_Widget {

/**
 * Constructor
 */
function category_posts_tab2_widget() {
		$widget_ops = array( 'classname' => 'category_posts_tab2_widget', 'description' => 'Hiển thị tin theo danh mục (đặt vào tab2).' ); // Widget Settings
		$control_ops = array( 'id_base' => 'category_posts_tab2_widget' ); // Widget Control Settings
		$this->WP_Widget( 'category_posts_tab2_widget', 'Category Posts tab2 Widget', $widget_ops, $control_ops ); // Create the widget
}

/**
 * Extract Args 
 * (non-PHPdoc)
 * @see WP_Widget::widget()
 */
function widget($args, $instance) {
			extract( $args );
			$title 		= apply_filters('widget_title', $instance['title']); // the widget title
			$cp_id		= $instance['cp_id']; // category id
			$postsnumber 		= $instance['postsnumber']; // number of posts to display
			// $rssid 		= $instance['rssid']; // rss feed link
			// $newsletterurl 	= $instance['newsletter_url'];  URL of newsletter signup
			// $authorcredit	= isset($instance['author_credit']) ? $instance['author_credit'] : false ; give plugin author credit
// Before widget //
			echo $before_widget;
	// Title of widget //
			if ( $title ) { echo $before_title . $title . $after_title; }
	// Widget output //
?>
<!--  Start list  -->
		<div class="catbox-row clear"><ul>
	   <?php			
	      query_posts('showposts='.$postsnumber.'&cat='.$cp_id);
	      $postcount = 0;
	      while (have_posts()) : the_post();
	      global $post;
				if($postcount == 0 ) {  ?>
					<li class="first">
            <blockquote> <div class="entry-meta">
              <i>
							<span class="meta-date"><?php the_time('D. d - M - Y'); ?></span>
              <span class="meta-sep">|</span> <?php echo 'Post by: '. get_the_author() ; ?>
              <span class="meta-sep">|</span>
							<span class="meta-comments"><?php comments_popup_link( __( '0 comment', 'themejunkie' ), __( '1 comment', 'themejunkie' ), __( '% comments', 'themejunkie' ) ); ?></span>
              </i>
            </div>
            </blockquote> <!--end .entry-meta-->
            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'themejunkie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) { the_post_thumbnail(array(200,160), array('class' => 'alignleft post_thumbnail thumbnail')); } ?></a>
						<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'themejunkie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
						
						<div class="entry-excerpt">
							<?php //the_excerpt( __( 'more <span class="meta-nav">&rarr;</span>', 'themejunkie' ) ); ?>
							<?php echo	 bm_better_excerpt(300,' ...'); ?>
						</div> <!--end .entry-excerpt-->				
					</li> <!--end .first-->
				<?php } else {
	                echo '<li class="catlist">
	                			<i><a href="'.get_permalink($post->ID).'" rel="bookmark"> <span style="color:red">&raquo;</span> &nbsp;'.$post->post_title .'</a></i></li>';
	            }
	            $postcount++;
	        endwhile;
			wp_reset_query();
?>
	</ul></div><!--end .catbox-->
<div class="clear"></div>
<!--  End List -->
<?php
	// After widget //
			echo $after_widget;
} 
/**
 * Update Settings 
 * (non-PHPdoc)
 * @see WP_Widget::update()
 */

function update($new_instance, $old_instance) {
 			$instance['title'] = strip_tags($new_instance['title']);
 			$instance['cp_id'] = strip_tags($new_instance['cp_id']);
			$instance['postsnumber'] = strip_tags($new_instance['postsnumber']);
 			return $instance;
 		}



/**
 * Widget Control Panel Form
 * (non-PHPdoc)
 * @see WP_Widget::form()
 */
function form($instance) {
  $defaults = array( 'title' => 'Category Posts', 'cp_id' => 'Category ID' );
	$instance = wp_parse_args( (array) $instance, $defaults ); ?>
 		<p>
 			<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
		 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
 		</p>
		<p>
      <label for="<?php echo $this->get_field_id('cp_id'); ?>"><?php _e('Category ID'); ?></label>
       <select id="<?php echo $this->get_field_id('cp_id'); ?>" name="<?php echo $this->get_field_name('cp_id'); ?>" > 
			 <option value="">Select a Category</option> 
 			<?php 
 				$category = $instance['cp_id'];
  				$categories=  get_categories(); 
  				foreach ($categories as $cat) {
  					$option = '<option value="'.$cat->term_id.'"';
  					if ($category == $cat->term_id) $option .= ' selected="selected">';
  					else { $option .= '>'; }
					$option .= $cat->cat_name;
					$option .= ' ('.$cat->category_count.')';
					$option .= '</option>';
					echo $option;
  				}
 			?>
			</select>
		</p> 		
		<p>
 			<label for="<?php echo $this->get_field_id('postsnumber'); ?>"><?php _e('Number of posts to display'); ?></label>
 			<input class="widefat" id="<?php echo $this->get_field_id('postsnumber'); ?>" name="<?php echo $this->get_field_name('postsnumber'); ?>" type="text" value="<?php echo $instance['postsnumber']; ?>" />
 		</p>
<?php }

}



// End class category_posts_widget
	add_action('widgets_init', create_function('', 'return register_widget("category_posts_tab2_widget");'));	
?>